package _18_剑指OfferII;

public class _008_剑指OfferII和大于等于target的最短子数组 {

    public static void main(String[] args) {

        _008_剑指OfferII和大于等于target的最短子数组 v = new _008_剑指OfferII和大于等于target的最短子数组();


        int[] ins = {1, 2, 3, 4, 5};

        System.out.println(v.minSubArrayLen(11, ins));

    }

    // 滑动窗口，寻找最大值
    public int minSubArrayLen(int target, int[] nums) {
        int len = nums.length;
        int right = 0;
        int total = 0;
        int min = len + 1;
        for (int i = 0; i < len; i++) {
            while (right < len && total < target) {
                total += nums[right++];
            }
            if (total >= target) {
                min = Math.min(min, right - i);
            }
            total -= nums[i];
        }
        return min == len + 1 ? 0 : min;
    }

}
